package edu.bw.service.impl;

import edu.bw.dao.DeptDao;
import edu.bw.dao.impl.DeptDaoImpl;
import edu.bw.dto.UpdateDeptForm;
import edu.bw.pojo.Dept;
import edu.bw.dto.SelectDeptByConditionForm;
import edu.bw.service.DeptService;
import edu.bw.utils.PageUtils;

import java.util.List;
import java.util.Map;

public class DeptServiceImpl implements DeptService {
    DeptDao deptDao=new DeptDaoImpl();
    @Override
    public PageUtils selectDeptByCondition(SelectDeptByConditionForm selectCondition) {
        //1.根据条件查询的数据
        List<Map<String, Object>> list = deptDao.selectDeptByCondition(selectCondition);

        //2.根据条件查询出总条数
        Long count = deptDao.selectDeptByConditionCount(selectCondition);

        int beginIndex = (selectCondition.getPage() - 1) * selectCondition.getLength();

        /**
         * list: 当前页的数据
         * count: 总条数
         * beginIndex: 起始索引
         * selectCondition.getLength(): 每页显示条数
         */
        return new PageUtils(list, count, beginIndex, selectCondition.getLength());
    }

    @Override
    public Integer insert(Dept insertDept) {
        return deptDao.insert(insertDept);
    }

    @Override
    public List<Map<String, Object>> selectById(Integer id) {
        return deptDao.selectById(id);
    }


    @Override
    public Integer update(UpdateDeptForm updateDeptForm) {
        return deptDao.update(updateDeptForm);
    }

    @Override
    public Integer deleteById(List<Integer> ids) {

        if(deptDao.selectCanDelete(ids)){
            return deptDao.delete(ids);
        }
        return 0;
    }

    @Override
    public List<Map<String, Object>> selectAllDept() {
        return deptDao.selectAllDept();
    }
}
